package student.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import student.aistar.model.bo.Result;
import student.aistar.model.entity.Navclass;
import student.aistar.model.entity.User;
import student.aistar.model.entity.WebSeckill;
import student.aistar.service.IIndexService;
import student.aistar.service.ITVSeckillService;

import javax.servlet.http.HttpSession;
import java.nio.channels.Channel;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.ui.Model;

/**
 * 本类用来演示:
 *
 * @author: qinFeng
 * @date: 2021/10/12 19:28
 */
@Controller
public class TVSeckillController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ITVSeckillService seckillService;

    @Autowired
    private IIndexService iIndexService;

    @PostMapping("/tvSeckill")
    @ResponseBody
    public Result tvKill(Integer killId, HttpSession session){
        //获取秒杀的状态
        String state = seckillService.getStateByKillId(killId);
        if(state.equals("0"))
            return new Result("400", "秒杀尚未开始");
        if(state.equals("2"))
            return new Result("500", "秒杀结束了");


        User user = (User) session.getAttribute("user");

        //校验redis里面是否还有该商品
        ListOperations lop = redisTemplate.opsForList();
        Object o = lop.leftPop("sk_" + killId);
        if(o == null)
            return new Result("400", "对不起, 当前商品已售罄");

        //判断是否重复抢购的用户
        SetOperations set = redisTemplate.opsForSet();
        boolean flag = set.isMember("userId" + killId, user.getUserId());
        if(flag){
            lop.leftPush("sk_" + killId, killId);
            return new Result("400", "别重复抢购");
        }else {
            set.add("userId" + killId, user.getUserId());
            //秒杀的信息放到rabbitmq里面
            seckillService.generateSpikeOrder(user.getUserId(), killId);
            return new Result("200", "恭喜抢购成功");
        }
    }

    @GetMapping("/tVSeckillJsp")
    public String tVSeckillJsp(Integer killId, Model model){
        List<Navclass> navClassList = iIndexService.navClassList();
        model.addAttribute("navClassList", navClassList);
        WebSeckill webSeckill = seckillService.getSeckillTime(killId);

        model.addAttribute("seckillTime",webSeckill.getStartTime().getTime());
        model.addAttribute("lastTime", webSeckill.getEndTime().getTime() - new Date().getTime());
        return "/mi/seckill/tvSeckill";
    }
}
